package com.deer.wms.base.system.web.bill;

import com.deer.wms.base.system.finalUtil.AreaInfoId;
import com.deer.wms.base.system.model.MaterialEdit.MaterialEdit;
import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.record.OperationRecord;
import com.deer.wms.base.system.model.zh.Quality;
import com.deer.wms.base.system.service.bill.IMaterialDetailService;
import com.deer.wms.base.system.service.bill.IMaterialInfoService;
import com.deer.wms.base.system.service.record.OperationRecordService;
import com.deer.wms.base.system.service.zh.QualityService;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.utils.DateUtils;
import com.deer.wms.common.utils.poi.ExcelUtil;
import com.deer.wms.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 入库单 信息操作处理
 *
 * @author guo
 * @date 2019-05-13
 */
@Controller
@RequestMapping("/in/MaterialDetail")
public class MaterialDetailController extends BaseController {

    private String prefix = "in/MaterialDetail";
    @Autowired
    private IMaterialInfoService iMaterialInfoService;

    @Autowired
    private IMaterialDetailService iMaterialDetailService;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QualityService qualityService;
    @Autowired
    private OperationRecordService operationRecordService;


    /**
     * 查询入库单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialDetailCriteria materialDetailCriteria) {
        startPage();
        List<MaterialDetail> list = iMaterialDetailService.findList(materialDetailCriteria);
        return getDataTable(list);
    }

    //修改
    @RequiresPermissions("in:MaterialDetail:edit")
    @GetMapping("edit/{materialId}")
    public String edit(@PathVariable String materialId, Model model) {
        MaterialDetailCriteria materialDetailCriteria = new MaterialDetailCriteria();
        materialDetailCriteria.setMaterialId(materialId);
        MaterialDetail mm = new MaterialDetail();
        List<MaterialDetailDto> materialDetailDtos = iMaterialDetailService.selectMaterialList(materialDetailCriteria);
        for (MaterialDetail m : materialDetailDtos
        ) {

            mm.setMaterialId(m.getMaterialId());
            mm.setMaterialProduct(m.getMaterialProduct());
            mm.setResult(m.getResult());
            mm.setId(m.getId());

        }
        model.addAttribute("MaterialDetail", mm);
        return prefix + "/edit";
    }

    //修改保存
    @PostMapping("/editsave")
    @ResponseBody
    @Transactional
    public AjaxResult editsave(MaterialDetail materialDetail) {
        String s = new String(materialDetail.getMaterialId());
        Quality code = qualityService.findBy("code", s);
        Quality q = new Quality();
        q.setQualityId(code.getQualityId());
        q.setResult(materialDetail.getResult());
        qualityService.update(q);

        MaterialDetail materialId = iMaterialDetailService.findBy("materialId", materialDetail.getMaterialId());
        Integer result = code.getResult();
        StringBuilder stb=new StringBuilder();
        if(result==0){
            stb.append("OK-");
        }else if(result==1){
            stb.append("NG-");

        }else if(result==3){
            stb.append("灰度异常-");

        }else if(result==4){
            stb.append("长度异常-");

        }else if(result==5){
            stb.append("宽度异常-");

        }else if(result==6){
            stb.append("位置异常-");

        }

        if(materialDetail.getResult()==0){
            stb.append("OK");

        }else if(materialDetail.getResult()==1){
            stb.append("NG");

        }else if(materialDetail.getResult()==3){
            stb.append("灰度异常");

        }else if(materialDetail.getResult()==4){
            stb.append("长度异常");

        }else if(materialDetail.getResult()==5){
            stb.append("宽度异常");

        }else if(materialDetail.getResult()==6){
            stb.append("位置异常");

        }
        String comment = materialId.getComment()+",";
        materialDetail.setComment(comment+stb.toString());

        iMaterialDetailService.update(materialDetail);



        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setCreateTime(DateUtils.getTime());
        operationRecord.setMan(ShiroUtils.getLoginName());
        operationRecord.setMaterialid(materialDetail.getMaterialId());
        operationRecord.setOperation("修改");
        operationRecordService.save(operationRecord);

        return toAjax(true);
    }


    /**
     * 查询入库单列表
     */
    @PostMapping("/findList")
    @ResponseBody
    @Transactional
    public TableDataInfo findList(String materialBatchId) {
        startPage();
        MaterialDetailCriteria materialDetailCriteria = new MaterialDetailCriteria();
        List<MaterialDetail> list = iMaterialDetailService.findList(materialDetailCriteria);
        return getDataTable(list);
    }

    /**
     * 晶棒入库详情
     *
     * @return com.deer.wms.common.core.page.TableDataInfo
     * @author 李帅辰
     * @date 2021/6/9 14:43
     */
    @RequiresPermissions("in:MaterialDetail:list")
    @PostMapping("/findMaterialDetailList")
    @ResponseBody
    @Transactional
    public TableDataInfo findMaterialDetailList(MaterialDetailCriteria criteria) {
        startPage();
        criteria.setAreaId(AreaInfoId.AREAINFO_CACHE_LINE);
        List<MaterialDetailDto> list = iMaterialDetailService.findMaterialDetailBoxList(criteria);
        return getDataTable(list);
    }
    //修改明细
    @RequestMapping("/editdetail")
    @ResponseBody
    public MaterialDetailDto editview(@RequestParam Integer id,Model model){
        MaterialDetailCriteria criteria=new MaterialDetailCriteria();
        criteria.setId(id);
        List<MaterialDetailDto> list = iMaterialDetailService.selectMaterialList(criteria);
        MaterialDetailDto materialDetailDto = list.get(0);
        return materialDetailDto;

    }
    //修改明细
    @RequestMapping("/editdetailsave")
    @ResponseBody
    public AjaxResult editsave(MaterialEdit materialEdit){
        int i = iMaterialDetailService.updatedetail(materialEdit);
        if(i>0){
            return toAjax(true);
        }else{
            return toAjax(false);
        }

    }


    @GetMapping("/page")
    public String MaterialInfo() {
        return prefix + "/MaterialInfo";
    }


    @GetMapping("/detail/{materialId}")
    public String billInDetail(@PathVariable("materialId") String materialId) {
        return prefix + "/";
    }


    /***
     *导出
     * @author 李帅辰
     * @date 2020/9/24 11:06
     * @return com.deer.wms.common.core.domain.AjaxResult
     */
    @RequiresPermissions("in:MaterialDetail:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialDetailCriteria criteria) {
       // List<MaterialInfo> list = iMaterialInfoService.findList(criteria);
        List<MaterialDetailDto> list = iMaterialDetailService.selectMaterialList(criteria);
        ExcelUtil<MaterialDetailDto> util = new ExcelUtil<MaterialDetailDto>(MaterialDetailDto.class);
        return util.exportExcel(list, "晶棒入库");
    }


    /**
     * 导出列表
     */
    @RequiresPermissions("in:MaterialDetail:partExcel")
    @PostMapping("/partExcel")
    @ResponseBody
    public AjaxResult partExcel(@RequestParam String ids) {
        List<MaterialInfo> byIds = new ArrayList();
        String[] split = ids.split(",");
        for (String s : split
        ) {
            MaterialInfoCriteria b = new MaterialInfoCriteria();
            b.setCarrierId(s);
            List<MaterialInfo> list = iMaterialInfoService.findList(b);
            for (MaterialInfo bs : list
            ) {
                byIds.add(bs);
            }


        }
        // List<BillInSapDto> billInSapList = billInSapService.findBillInSapDtoList(billInSapCriteria);
        ExcelUtil<MaterialInfo> util = new ExcelUtil<MaterialInfo>(MaterialInfo.class);
        return util.exportExcel(byIds, "晶棒托入库报表");
    }
    /**
     * 新增保存入库单
     */
//    @RequiresPermissions("in:materialInfo:add")
//    @Log(title = "入库单", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public Result addSave(materialInfo materialInfo) {
//        materialInfo.setCreateUserName(ShiroUtils.getLoginName());
//        materialInfo.setCreateUserId(ShiroUtils.getUserId());
//        iMaterialInfoService.save(materialInfo);
//  //      iMaterialInfoService.insertmaterialInfo(materialInfo);
////        BillInDetail billInDetail = new BillInDetail();
////        billInDetail.setBillId(materialInfo.getBillId());
////        billInDetailService.saveBillInDatails(billInDetail);
//        return ResultGenerator.genSuccessResult();
//    }


    /**
     * 新增入库单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


}
